package com.xqboss.web.controller.nft;


import com.xqboss.apps.domain.nft.Category;
import com.xqboss.apps.dto.category.CategoryDto;
import com.xqboss.apps.vo.category.CategoryVo;
import com.xqboss.apps.vo.nft.BrandVO;
import com.xqboss.apps.vo.nft.CategoryVO;
import com.xqboss.apps.enums.category.CategoryTypeEnum;
import com.xqboss.apps.service.nft.BrandService;
import com.xqboss.apps.service.nft.CategoryService;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.AjaxResult;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.enums.BusinessType;
import com.xqboss.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Administrator
 */
@Api(tags = "系统-类别管理")
@RestController
@RequestMapping("/web/category")
public class WebCategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BrandService brandService;

    @PreAuthorize("@ss.hasPermi('web:category:tree-list')")
    @ApiOperation("类别标签列表")
    @GetMapping("/tree-list")
    public R<List<CategoryVO>> treeList(@RequestParam(name = "targetType",required = false,defaultValue = "1") @ApiParam("标签类型") CategoryTypeEnum targetType) {
        List<CategoryVO> list = categoryService.treeList(targetType.getValue());
        return R.ok(list);
    }

    /**
     * 查询标签列表
     */
    @PreAuthorize("@ss.hasPermi('web:category:list')")
    @ApiOperation("查询标签")
    @GetMapping("/list")
    public R<List<Category>> list(Category category) {
        startPage();
        List<Category> list = categoryService.selectCategoryList(category);
        return R.ok(list);
    }

    /**
     * 导出标签列表
     */
    @PreAuthorize("@ss.hasPermi('web:category:export')")
    @ApiOperation("导出标签")
    @Log(title = "标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Category category) {
        List<Category> list = categoryService.selectCategoryList(category);
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        util.exportExcel(response, list, "分类数据");
    }

    /**
     * 获取标签详细信息
     */
    @PreAuthorize("@ss.hasPermi('web:category:get')")
    @ApiOperation("获取标签列表")
    @GetMapping(value = "/{id}")
    public R<Category> getInfo(@PathVariable("id") Integer id) {
        return R.ok(categoryService.selectCategoryById(id));
    }

    /**
     * 新增标签
     */
    @PreAuthorize("@ss.hasPermi('web:category:add')")
    @ApiOperation("新增标签")
    @Log(title = "标签", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Category category) {
        return toAjax(categoryService.insertCategory(category));
    }

    /**
     * 修改标签
     */
    @PreAuthorize("@ss.hasPermi('web:category:edit')")
    @ApiOperation("修改标签")
    @Log(title = "标签", businessType = BusinessType.UPDATE)
    @PutMapping("edit")
    public AjaxResult edit(@RequestBody Category category) {
        return toAjax(categoryService.updateCategory(category));
    }

    /**
     * 删除标签
     */
    @PreAuthorize("@ss.hasPermi('web:category:delete')")
    @ApiOperation("删除标签")
    @Log(title = "标签", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(categoryService.deleteCategoryByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('web:category:brand-tree-list')")
    @ApiOperation("获取品牌标签树结构v23.8.8.2")
    @GetMapping("/brand-tree-list")
    public R<List<BrandVO>> brandTreeList(@RequestParam(name = "targetType",required = false,defaultValue = "1") @ApiParam("标签类型") CategoryTypeEnum targetType) {
        List<BrandVO> list = brandService.getTreeList(targetType.getValue());
        return R.ok(list);
    }

    //****************************************************************************************************************
    //**************************************下面是v1.7.5新增的 藏品/装备/宝盒 分类功能**************************************
    //****************************************************************************************************************

    /**
     * 查询分类列表
     */
    @PreAuthorize("@ss.hasPermi('web:category:listNft')")
    @ApiOperation("v1.7.5查询分类（1藏品 2装备 3箱子）")
    @GetMapping("/listNft")
    public R<List<CategoryVo>> listNft(@Validated CategoryDto dto) {
        if (dto.getIsPage())dto.startPageAndOrder();
        List<CategoryVo> list = categoryService.selectCategoryListNft(dto);
        return R.ok(list);
    }


    /**
     * 新增分类
     */
    @PreAuthorize("@ss.hasPermi('web:category:addNft')")
    @ApiOperation("v1.7.5新增分类")
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @PostMapping("/addNft")
    public AjaxResult addNft(@RequestBody Category category) {
        return toAjax(categoryService.insertCategoryNft(category));
    }

    /**
     * 修改分类
     */
    @PreAuthorize("@ss.hasPermi('web:category:editNft')")
    @ApiOperation("v1.7.5修改分类")
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PutMapping("editNft")
    public AjaxResult editNft(@RequestBody Category category) {
        return toAjax(categoryService.updateCategoryNft(category));
    }

    /**
     * 删除分类
     */
    @PreAuthorize("@ss.hasPermi('web:category:deleteNft')")
    @ApiOperation("v1.7.5删除分类")
    @Log(title = "分类", businessType = BusinessType.DELETE)
    @DeleteMapping("delNft/{id}")
    public AjaxResult removeNft(@PathVariable Integer id) {
        return toAjax(categoryService.deleteCategoryNftById(id));
    }
}